package cqie.cn.frame.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-09-22 周三
 * @time 10:12
 * @description
 */
public class CodeGenerator {
    public static void main(String[] args) {

        AutoGenerator ag = new AutoGenerator();

        //全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");

        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("wangLin");
        gc.setOpen(false);
        //是否覆盖
        gc.setFileOverride(false);
        gc.setBaseResultMap(true);
        gc.setServiceName("I%sService");
        gc.setIdType(IdType.AUTO);
        gc.setDateType(DateType.ONLY_DATE);
        //实体属性 Swagger2 注解
        gc.setSwagger2(true);

        ag.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/frame_db?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        ag.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
       // pc.setModuleName(scanner("模块名"));
        pc.setModuleName("frame");
        pc.setParent("cqie.cn");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        ag.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //设置要映射表名
        strategy.setInclude("sys_user", "sys_menu", "sys_role", "role_menu", "user_role");
//        strategy.setInclude(new String[]{"sys_user", "sys_menu"});
//        strategy.setInclude("sys_user");
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setRestControllerStyle(true);
        //自动设置lombok
        strategy.setEntityLombokModel(true);
        // 自定义实体父类
        strategy.setSuperEntityClass("com.baomidou.mybatisplus.extension.activerecord.Model");
//        strategy.setSuperEntityClass("cqie.cn.frame.entity.BaseEntity");
//        strategy.setSuperControllerClass("cqie.cn.frame.controller.BaseController");
//        strategy.setLogicDeleteFieldName("status"); //逻辑删除
//        strategy.setTablePrefix("t_"); //设置表前缀
        strategy.setLogicDeleteFieldName("status");
        //自动填充
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT);
        List<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategy.setTableFillList(tableFills);
        //设置乐观锁
//        strategy.setVersionFieldName("version");

        strategy.setRestControllerStyle(true);
//        strategy.setControllerMappingHyphenStyle(true);// localhost:8080/hello_id_2
        ag.setStrategy(strategy);

        ag.execute(); //执行
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~自动代码生成~~~~~~~~~~~~~~~~~~~~~~~~~");


    }
}
